//
//  WholeCountryViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/17.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "WholeCountryViewController.h"
#import "BaseHeader.h"
@interface WholeCountryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation WholeCountryViewController
-(void)dealloc
{
    [_url release];
    [_inforArr release];
    [_collection release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _inforArr = [[NSMutableArray alloc]init];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.page = 1;

    [self creatNavigation];
    [self getTheData:1];

}
-(void)creatNavigation
{
    UIView *nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    nav.userInteractionEnabled = YES;
    nav.backgroundColor = [UIColor colorWithRed:250./255 green:250./255 blue:250./255 alpha:.1];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 30)];
    title.text = @"全部城市";
    title.textAlignment = YES;
    [nav addSubview:title];
    
    UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    backBut.frame = CGRectMake(10, 20, 30, 30);
    [backBut setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(gobackView) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:backBut];
    [self.view addSubview:nav];
    
    [nav release];
}
-(void)gobackView
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)getTheData:(int)number
{
     NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=%d&track_app_version=6.6.6&track_user_id=6695170&v=1",self.url,number];
    [AFNblock getdataWithUrlString:str dataBlock:^(id block) {
        
        [self dealDataWithDic:block];
    } netBlock:^(id block) {
        
        [self dealDataWithDic:block];
    }];
}
-(void)dealDataWithDic:(NSDictionary *)block
{
    NSDictionary *bigDic = block;
    NSArray *dataArr = [bigDic objectForKey:@"data"];
    for (int i = 0; i < dataArr.count; i++) {
        NSDictionary *dic = [dataArr objectAtIndex:i];
        WholeCountryModel *whole = [[WholeCountryModel alloc]initWithDictionary:dic];
        [_inforArr addObject:whole];
    }
    if (_collection == nil) {
        
        [self creatCollectionView];
    }
    [_collection reloadData];
    [_collection.footer endRefreshing];
}
-(void)creatCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(SCREEN_WIDTH/7*3.1, SCREEN_WIDTH/7*4);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.sectionInset = UIEdgeInsetsMake(SCREEN_WIDTH/35, SCREEN_WIDTH/25, SCREEN_WIDTH/35, SCREEN_WIDTH/25);
    
    _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flow];
    _collection.backgroundColor = [UIColor colorWithRed:240./255 green:240./255 blue:240./255 alpha:1];
    [_collection setDelegate:self];
    [_collection setDataSource:self];
    [self.view addSubview:_collection];
    
    [_collection registerClass:[WholeCountryCollectionViewCell class] forCellWithReuseIdentifier:@"WholeCountry"];
    [self  shangla];
    

    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _inforArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WholeCountryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WholeCountry" forIndexPath:indexPath];
    WholeCountryModel *mod = [_inforArr objectAtIndex:indexPath.row];
    [cell setValueWithModel:mod];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllCountViewController *city = [[AllCountViewController alloc]init];
    WholeCountryModel *cityMod = [_inforArr objectAtIndex:indexPath.row];
    city.urlStr = cityMod.cid;
    [self.navigationController pushViewController:city animated:YES];
    
}
-(void)shangla
{
    __block WholeCountryViewController *wholeC = self;
    [_collection addLegendFooterWithRefreshingBlock:^{

        wholeC.page += 1;
        [wholeC getTheData:wholeC.page];

    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
